package com.wolfalpha.utron.activity;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.wolfalpha.utron.R;
import com.wolfalpha.utron.activity.base.BaseNormalActivity;
import com.wolfalpha.utron.adapter.ItemIdeaAdapter;
import com.wolfalpha.utron.app.AppConfig;
import com.wolfalpha.utron.app.AppContext;
import com.wolfalpha.utron.bean.ItemIdea;
import com.wolfalpha.utron.service.IdeaService;
import com.wolfalpha.utron.service.impl.IdeaServiceImpl;
import com.wolfalpha.utron.ui.UIHelper;
import com.wolfalpha.utron.utils.ThreadPoolUtils;
import com.wolfalpha.utron.view.AlertDialog;
import com.wolfalpha.utron.view.list.WaterDropListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 我的全部想法
 * <p/>
 * Created by luodon on 15/8/19.
 */
public class MyIdeasActivity extends BaseNormalActivity implements WaterDropListView.IWaterDropListViewListener {

    private WaterDropListView listView;
    private IdeaService ideaService;


    private List<ItemIdea> ideas;
    private ItemIdeaAdapter adapter;

    private int userId = -1;
    private String username = "";

    private boolean self = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Bundle bundle = getIntent().getExtras();
        if (bundle == null || !bundle.containsKey("userId")) {
            userId = AppContext.getInstance().getUserId();
            self = true;
        } else {
            userId = bundle.getInt("userId");
            username = bundle.getString("userName");
            self = false;
        }
        setContentView(R.layout.activity_my_ideas);

        intiView();
        loadData();
    }

    private int page = 1;

    @Override
    public void onRefresh() {
        page = 1;

        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {

                ideas = ideaService.getMyList(userId, page);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (ideas != null) {
                            adapter.setData(ideas);
                            listView.stopLoadMore();
                            if (ideas.size() < AppConfig.LIST_PAGE_SIZE) {
                                listView.setPullLoadEnable(false);
                            } else {
                                listView.setPullLoadEnable(true);
                            }
                        } else {
                            listView.setPullLoadEnable(false);
                        }
                        listView.stopRefresh();
                    }
                });
            }
        });

    }

    @Override
    public void onLoadMore() {
        ThreadPoolUtils.execute(new Runnable() {
            @Override
            public void run() {
                final List<ItemIdea> mideas = ideaService.getMyList(userId, ++page);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (mideas != null) {
                            ideas.addAll(mideas);
                            adapter.setData(ideas);
                            if (mideas.size() < AppConfig.LIST_PAGE_SIZE) {
                                listView.setPullLoadEnable(false);
                            } else {
                                listView.setPullLoadEnable(true);
                            }
                        } else {
                            page--;
                            listView.setPullLoadEnable(false);
                        }
                        listView.stopLoadMore();
                    }
                });
            }
        });
    }

    /**
     * 加载列表的数据
     */
    private void loadData() {
        ideas = new ArrayList<ItemIdea>();
        adapter = new ItemIdeaAdapter(MyIdeasActivity.this, ideas);
        listView.setAdapter(adapter);
        onRefresh();
    }

    /**
     * 初始化界面
     */
    private void intiView() {
        if (userId == AppContext.getInstance().getUserId()) {
            setTvTitle("我的灵感");
        } else {
            setTvTitle(username + "的灵感");
        }

        listView = (WaterDropListView) findViewById(R.id.wl_idea);
        ideaService = new IdeaServiceImpl();
        listView.setWaterDropListViewListener(this);
        listView.setPullLoadEnable(false);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position < 1 || position > ideas.size()) {
                    //过滤刷新头部
                    return;
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("ideaId", ideas.get(position - 1).getId());
                    UIHelper.showActivity(MyIdeasActivity.this, IdeaDetailActivity.class, bundle);
                }
            }
        });

        listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int i, final long l) {
                if (!self) return false;
                final AlertDialog alertDialog = new AlertDialog(MyIdeasActivity.this);
                alertDialog.setTitle("警告");
                alertDialog.setMessage("即将删除该创意?");
                alertDialog.setNegativeButton("点错了", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        alertDialog.dismiss();
                    }
                });
                alertDialog.setPositiveButton("删除", new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (i < 1) return;
                        final ProgressDialog pd = new ProgressDialog(MyIdeasActivity.this);
                        pd.setMessage("正在删除...");
                        pd.show();
                        ThreadPoolUtils.execute(new Runnable() {
                            @Override
                            public void run() {
                                final boolean r = ideaService.delete(ideas.get(i - 1).getId());
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        pd.dismiss();
                                        alertDialog.dismiss();
                                        if (r) {
                                            Toast.makeText(MyIdeasActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                                            ideas.remove(i - 1);
                                            adapter.notifyDataSetChanged();
                                        } else {
                                            Toast.makeText(MyIdeasActivity.this, "删除失败,请重试", Toast.LENGTH_SHORT).show();
                                        }
                                    }
                                });
                            }
                        });

                    }
                });
                return true;

            }
        });


    }

}
